<html>
<head><meta charset="utf-8"><title>Macro error when using negative literals · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html">Macro error when using negative literals</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214298694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214298694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> weegee <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214298694">(Oct 23 2020 at 10:57)</a>:</h4>
<p>From this issue <a href="https://github.com/rust-analyzer/rust-analyzer/issues/6292">https://github.com/rust-analyzer/rust-analyzer/issues/6292</a> I would like to pick this one up. Any directions or head starts?</p>



<a name="214300717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214300717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214300717">(Oct 23 2020 at 11:24)</a>:</h4>
<p>I'm pretty sure the error means that the macro didn't expand fully, and my guess as to why would be that <code>-1.0</code> doesn't get matched as a literal because (I'm guessing) it probably gets parsed as two tokens which would form a unary expression</p>



<a name="214300929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214300929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214300929">(Oct 23 2020 at 11:26)</a>:</h4>
<p>Yeah, according to the spec <code>$x:literal</code> should match "<code>-</code>? <em>LiteralExpression</em>" <a href="https://doc.rust-lang.org/reference/macros-by-example.html#metavariables">https://doc.rust-lang.org/reference/macros-by-example.html#metavariables</a></p>



<a name="214301323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214301323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> weegee <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214301323">(Oct 23 2020 at 11:31)</a>:</h4>
<p>so it gets parsed as two tokens one <code>-</code> and other <code>1.0</code> right? (When it should be a single literal <code>-1.0</code>) So where's the parsing for literals done?</p>



<a name="214301650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214301650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214301650">(Oct 23 2020 at 11:35)</a>:</h4>
<p>it's correct that it's being parsed that way, probably our definition of a literal fragment is wrong (according to what Jonas wrote)</p>



<a name="214301841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214301841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214301841">(Oct 23 2020 at 11:37)</a>:</h4>
<p>I think the relevant code is <a href="https://github.com/rust-analyzer/rust-analyzer/blob/81609960fa30ea92e37b23dc7b025d1939626812/crates/mbe/src/mbe_expander/matcher.rs#L428">this function</a>, which implements literals as just parsing a single literal token</p>



<a name="214301931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214301931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214301931">(Oct 23 2020 at 11:38)</a>:</h4>
<p>instead, literal will probably need to be a <code>FragmentKind</code> and handled by <code>expect_fragment</code></p>



<a name="214323575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214323575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> weegee <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214323575">(Oct 23 2020 at 14:53)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/81609960fa30ea92e37b23dc7b025d1939626812/crates/parser/src/lib.rs#L99">https://github.com/rust-analyzer/rust-analyzer/blob/81609960fa30ea92e37b23dc7b025d1939626812/crates/parser/src/lib.rs#L99</a> I saw this, do I have to include some seperate variants to proceed?</p>



<a name="214327139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Macro%20error%20when%20using%20negative%20literals/near/214327139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Macro.20error.20when.20using.20negative.20literals.html#214327139">(Oct 23 2020 at 15:22)</a>:</h4>
<p><span class="user-mention" data-user-id="357151">@weegee</span> yeah, I think you'll need to add a new function to <code>grammar::fragments</code> that first parses an optional <code>-</code>, and then a literal (<code>atom::literal</code>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>